//
//  CZEmoticonToolBarView.swift
//  Weibo24
//
//  Created by apple on 16/3/7.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
/*
    底部放 4个按钮 
    采用第一种方法: 添加约束
    采用第二种方法: UIStackView

*/

enum CZEmoticonToolBarButtonType: Int{

    case Recent = 10000
    case Default = 10001
    case Emoji = 10002
    case Lxh = 10003
    
}

// 声明一个协议
protocol CZEmoticonToolBarViewDelegate: NSObjectProtocol{
// 定义了一个方法
    func didToolBarItemClick(type : CZEmoticonToolBarButtonType)
    
}

class CZEmoticonToolBarView: UIView {
// 声明一个代理属性
    weak var delegate: CZEmoticonToolBarViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:--添加控件和设置属性
    private func setupUI(){
        
        addSubview(recentButton)
        addSubview(defaultButton)
        addSubview(emojiButton)
        addSubview(lxhButton)
        
        // 添加约束
        recentButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(self.snp_bottom)
            
            make.width.equalTo(defaultButton.snp_width)
        }
        
        defaultButton.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(recentButton.snp_right)
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(self.snp_bottom)
            
            make.width.equalTo(emojiButton.snp_width)
            
        }
        
        emojiButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(defaultButton.snp_right)
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(self.snp_bottom)
            
            make.width.equalTo(lxhButton.snp_width)
        }
        
        lxhButton.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(emojiButton.snp_right)
            
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(self.snp_bottom)
            
            make.right.equalTo(self.snp_right)
            
        }
        
    }
    
    
    /*
        2种实现方法
        第一种方法 -- 定义一个全局的按钮,来记录
        第二种: 这种方式
    */
    @objc private func changeEmoticon(button: UIButton){
    
        
        for subView in self.subviews{
        
            let button = subView as! UIButton
            
            button.selected = false
            
        }
        
        button.selected = true
        
        //区分button
        // 根据 rawValue 的值,自动转换成 枚举类型
        
        delegate?.didToolBarItemClick(CZEmoticonToolBarButtonType(rawValue: button.tag)!)
        
    }
    
    
    //MARK:--懒加载
    
    private lazy var recentButton: UIButton = UIButton(title: "最近", image: "compose_emotion_table_normal", selectedImage: "compose_emotion_table_selected", tag: CZEmoticonToolBarButtonType.Recent.rawValue, target: self, action: "changeEmoticon:")
    
    private lazy var defaultButton: UIButton = UIButton(title: "默认", image: "compose_emotion_table_normal", selectedImage: "compose_emotion_table_selected", tag: CZEmoticonToolBarButtonType.Default.rawValue, target: self, action: "changeEmoticon:")
    
    private lazy var emojiButton: UIButton = UIButton(title: "emoji", image: "compose_emotion_table_normal", selectedImage: "compose_emotion_table_selected", tag: CZEmoticonToolBarButtonType.Emoji.rawValue, target: self, action: "changeEmoticon:")
    
//    private lazy var lxhButton: UIButton = UIButton(title: "浪小花", image: "compose_emotion_table_normal", selectedImage: "compose_emotion_table_selected", tag: CZEmoticonToolBarButtonType.Lxh.rawValue, target: self, action: "changeEmoticon:")
    private lazy var lxhButton: UIButton = CZFactory.getButton("浪小花", image: "compose_emotion_table_normal", selectedImage: "compose_emotion_table_selected", tag: CZEmoticonToolBarButtonType.Lxh.rawValue, target: self, action: "changeEmoticon:")
    /*
    private lazy var recentButton: UIButton = {
    
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setTitle("最近", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        
        button.tag = CZEmoticonToolBarButtonType.Recent.rawValue
        
        button.addTarget(self, action: "changeEmoticon:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
   //默认表情
    private lazy var defaultButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setTitle("默认", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        
        button.tag = CZEmoticonToolBarButtonType.Default.rawValue
        
        button.addTarget(self, action: "changeEmoticon:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    
    //emoji
    private lazy var emojiButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setTitle("emoji", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        
        button.tag = CZEmoticonToolBarButtonType.Emoji.rawValue
        
        button.addTarget(self, action: "changeEmoticon:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    
    //浪小花
    private lazy var lxhButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setTitle("浪小花", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        
        button.tag = CZEmoticonToolBarButtonType.Lxh.rawValue
        
        button.addTarget(self, action: "changeEmoticon:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
*/
}
